package com.example.tellwin;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.apkfuns.log2file.LogFileEngineFactory;
import com.apkfuns.logutils.LogLevel;
import com.apkfuns.logutils.LogUtils;
import com.example.tellwin.base.BaseApiModule;
import com.example.tellwin.base.BaseAppComponent;
import com.example.tellwin.base.BaseAppModule;
import com.example.tellwin.base.DaggerBaseAppComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.tic.TICManager;

import java.util.ArrayList;
import java.util.List;

import androidx.multidex.MultiDex;


public class MainApplication extends Application {

    private static Context context;
    public static float sScale;
    private static BaseAppComponent appComponent;

    public static String Identity = Common.NORMAL;

    private static List<Activity> activityList = new ArrayList<>();

    private final String TAG = "MainApplication";

    private static TICManager mTIC;

    public static TICManager getTICManager() {
        return mTIC;
    }

    private static MainApplication mainApplication;

    @Override
    public void onCreate() {
        super.onCreate();
        mainApplication = this;
        context = getApplicationContext();
        MultiDex.install(this);
        sScale = getResources().getDisplayMetrics().density;
        initCompoent(context);
        AppHelper.getInstance().init(this);
        Identity = AppHelper.getInstance().isAnswer() ? Common.ANSWER : Common.NORMAL;
        mTIC = TICManager.getInstance();
        mTIC.init(this, Common.SDKAPPID);

        // LogUtils.getLogConfig().configAllowLog(true).configShowBorders(false).configFormatTag("twjf");
        LogUtils.getLogConfig()
                .configAllowLog(true)  // 是否在Logcat显示日志
                .configShowBorders(false) // 是否显示边框
                .configLevel(LogLevel.TYPE_VERBOSE); // 配置可展示日志等级
        LogUtils.getLog2FileConfig().configLog2FileEnable(true)
                // targetSdkVersion >= 23 需要确保有写sdcard权限
                .configLog2FilePath("/sdcard/"+getResources().getString(R.string.app_name)+"/logs/")
                .configLog2FileNameFormat("%d{yyyyMMdd}.txt")
                .configLog2FileLevel(LogLevel.TYPE_VERBOSE) // 文件日志等级
                .configLogFileEngine(new LogFileEngineFactory(this));


      //  LogUtils.getLog2FileConfig().configLogFileEngine(new LogFileEngineFactory(context));
    }

    public static MainApplication getInstance() {
        return mainApplication;
    }

    public static Context getContext() {
        return context;
    }

    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }

    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
    }

    public static Activity getCurActivity() {
        if (activityList.size() == 0) {
            return null;
        }
        return activityList.get(activityList.size() - 1);
    }

    public static List<Activity> getActivityList() {
        return activityList;
    }

    public static BaseAppComponent getAppComponent() {
        return appComponent;
    }

    private static void initCompoent(Context context) {
        appComponent = DaggerBaseAppComponent.builder()
                .baseApiModule(new BaseApiModule())
                .baseAppModule(new BaseAppModule(context))
                .build();
    }

}
